package com.cloud.easy.im.handler.strategy.websocket;

import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.argeement.AgreementType;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.processor.CommonProcessor;
import com.cloud.easy.im.processor.OnlineProcessor;
import com.cloud.easy.im.utils.LocalSendHelper;
import com.cloud.easy.im.handler.strategy.AgreementStrategy;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 客户端发送通用拉取数据(拉取场景，只是适用于1vN场景)
 *
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月20日3:54 下午
 */
public class AgreementWebSocketCCommonPullHandler extends AgreementStrategy {

    private static final Logger logger = LoggerFactory.getLogger(AgreementWebSocketCCommonPullHandler.class);

    private final CommonProcessor commonProcessor;

    public AgreementWebSocketCCommonPullHandler() {
        super(AgreementType.C.FROM_CLIENT_TYPE_OF_COMMON$PULL);
        commonProcessor = CommonProcessor.getInstance();
    }

    @Override
    public void handler(Channel session, Agreement dataFromClient, ServerEventListener serverEventListener) throws Exception {
        logger.debug("[WS_GROUP$DATA]<< 收到客户端的发送群组消息请求.");

        if (serverEventListener != null) {
            //如果发送者不在线
            if (!OnlineProcessor.isOnline(session)) {
                LocalSendHelper.replyDataForUnlogined(session, dataFromClient, null);
                return;
            }
            commonProcessor.processC2CGroupMessage(dataFromClient, serverEventListener);
        } else {
            logger.warn("[WS_GROUP$DATA]<< 收到客户端的群发数据传输消息，但回调对象是null，回调无法继续.");
        }

    }
}
